<?php

/**
 * Created by PhpStorm.
 * User: Aistt
 * Date: 2016/3/4
 * Time: 11:39
 */
use Admin\Model\MenuModel;
use Library\ColumnUtils;
use Base\Controller\BaseAdminController;


/**
 * @param $table
 * @param $id
 * @param $field
 * @return mixed 获取一条数据
 * 获取一条数据
 */
function getTableRowOrField($table, $id, $field,$status)
{
    if (!$id) return null;
    if($status){
        $where['status']=$status;
    }
   $where['id']=$id;
    $sel = M($table)->where($where)->find();
//        echo  M($table)->_sql();
//    dump($sel);
    return $field ? $sel[$field] : $sel;
}

/**
 * 判断是否父栏目
 * @param int $id 子栏目
 * @param int $id2 父栏目
 * @return bool
 */
function isPro($id, $id2)
{
    if ($id == $id2) {
        return true;
    }
    $model = M('column');
    $sel1 = $model->find($id);
    $sel2 = $model->find($id2);
    if (empty($sel1) || empty($sel2)) {
        return false;
    }
    if ($sel1['column_id'] == $id2) {
        return true;
    } else {
        return isPro($sel1['column_id'], $id2);
    }
}

/**
 * 判断是否父目录
 * @param int $id 子栏目
 * @param int $id2 父栏目
 * @return bool
 */
function isProCatalog($id, $id2)
{
    if ($id == $id2) {
        return true;
    }
    $model = M('catalog');
    $sel1 = $model->find($id);
    $sel2 = $model->find($id2);
    if (empty($sel1) || empty($sel2)) {
        return false;
    }
    if ($sel1['catalog_id'] == $id2) {
        return true;
    } else {
        return isProCatalog($sel1['catalog_id'], $id2);
    }
}

//判断是否父栏目
function isParentCol($id){
    $model = M('column');
    $map['column_id']=$id;
    $map['status']=1;
    $data=$model->where($map)->select();
    if (!$data){
        return true;
    }else{
        return false;
    }
}
//查询栏目名称
function getColumnName($id){
    if($id){
        $map['status'] = 1;
        $map['id'] = $id;
        $data=M('column')->where($map)->find();
        return $data['title'];
    }
}

//查询栏目类型
function getColumnModel($id)
{
    if($id){
        $data = M('column')->where('id=' . $id)->find();
        return $data['model_id'];
    }
}

function isProMenu($id, $id2)
{
    if ($id == $id2) {
        return true;
    }
    $model = M('menu');
    $sel1 = $model->find($id);
    $sel2 = $model->find($id2);
    if (empty($sel1) || empty($sel2)) {
        return false;
    }
    if ($sel1['menu_id'] == $id2) {
        return true;
    } else {
        return isProMenu($sel1['menu_id'], $id2);
    }
}

//获取子栏目列表
function getSubColumn($columnId,$limit){
    $model = M('column');
    $data = $model->where(array('status' => 1, 'column_id' => $columnId))->limit($limit)->select();
    
    return $data;
}

/**
 * 读取数据列表
 * @param string $tableName 表名称
 * @param bool $relation
 * @param string $order
 * @param string $limit
 * @return array 返回数据集合
 */
function getList($tableName,  $relation = false, $order = 'id desc', $limit = '')
{
    if ($relation) {
        if ($limit) {
            $select = D($tableName)->relation(true)->order($order)->limit($limit)->select();
        } else {
            $select = D($tableName)->relation(true)->order($order)->select();
        }
    } else {
        if ($limit) {
            $select = M($tableName)->order($order)->limit($limit)->select();
        } else {
            $select = M($tableName)->order($order)->select();
        }
    }
    return $select;
}

/**
 * 菜单树状图
 * @param int $id
 * @param int $level
 * @return array|mixed
 */
function getMenuTree($id = 0, $level = 0)
{
    ColumnUtils::clear();
    $menuModel = new MenuModel();
    $list = $menuModel->relation(true)->where('status=1')->select();
    if ($id) {
        foreach ($list as $key => $value) {
            if (!isProMenu($value['id'], $id)){
                unset($list[$key]);
            } else{
                $list[$key]['sa'] = $value['id'];
            }
        }
    }
    $list = ColumnUtils::toTree($list, 'menu_id', $id, $level);
    return $list;
}

/**
 * 科室树状图
 * @return array|mixed
 */
function getDepartmentinfoTree()
{
    \Library\Utils::clear();
    $departmentModel = new \Admin\Model\DepartmentinfoModel();
    $list = $departmentModel->relation(true)->where('status=1')->select();
    $list = \Library\Utils::toTrees($list,'pid');
    return $list;
}

/**
 * 获取配置
 * @param $key
 */
function getConfig($key)
{
    $map = array(
        'key' => $key,
    );
    $c = M('config')->where($map)->find();
    return $c['value'];
}

/**
 * 截取utf-8字符串
 * @since 2008.12.23
 * @param string $str 被截取的字符串
 * @param integer $start 起始位置
 * @param integer $length 截取长度(每个汉字为3字节)
 * @return string
 */
function utf8_strcut($str, $start, $length = null)
{

    $str = strip_tags(htmlspecialchars_decode($str));
    $str = str_replace('&nbsp;', '', $str);
    preg_match_all('/./us', $str, $match);
    $chars = is_null($length) ? array_slice($match[0], $start) : array_slice($match[0], $start, $length);

    unset($str);

    return implode('', $chars);
}

/**
 * 判断是否手机访问
 * @return bool
 */
function isMobile()
{
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return true;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA'])) {
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    // 脑残法，判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
            'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp',
            'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry',
            'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm',
            'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
        );
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    // 协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;
}

//获取用户信息
function getUserInfo(){
    $base = new BaseAdminController();
    $user=$base->getUserInfo();

    return $user;
}

/**
 * @return mixed
 * 获取省区列表
 */
 function getProvinceList($type){
    if($type==1){
        $AerainfoModel= new \Admin\Model\AerainfoModel();
        $list = $AerainfoModel->relation(false)->field("aerainfo_id,aerainfo_name")->where(array('PARENT_ID'=>1,'status'=>1))->select();
        return $list;
    }else{
		$where['status']=1;
        $AerainfoModel= new \Admin\Model\AerainfoModel();
        $list = $AerainfoModel->relation(false)->field("aerainfo_id,aerainfo_name")->where($where)->select();
        return $list;
    }
}
/**
 * @return mixed
 * 获取身体部位列表
 */
function getBodyList(){
    $bodyModel = new \Admin\Model\BodyinfoModel();
    $list = $bodyModel->relation(false)->select();
    return $list;
}

/**
 * @return mixed
 * 获取市区列表
 */
function getaerainfoList(){
    $AerainfoModel=new \Admin\Model\AerainfoModel();
    $AeraList=$AerainfoModel->relation(true)->field('aerainfo_id,aerainfo_name')->where(array('PARENT_ID'=>1,'status'=>1))->select();
    return $AeraList;
}
function ArrayTo($disease){
    $disea=json_decode($disease);
    $dis="";
    for($i=0;$i<count($disea);$i++){
        $dis.=$disea[$i] . ' , ';
    }
    $data=substr($dis,0,-2);
    return $data;
}

function GetReviewed($content){
    $data = json_decode($content,true);
    $reviewed='未通过';
//    $result='';
    foreach ($data as $k=>$v){
//        $result.=$v['reviewed'].',';
        if($v['reviewed']==1){
            $reviewed='通过';
            return  $reviewed;
//           return '内容：'.$result.'，结果'.$reviewed;
        }else{
            $reviewed='未通过';
        }
    }
    return  $reviewed;
//    return '内容：'.$result.'，结果'.$reviewed;
}
/**
 * @param $user_id
 * @param $user_type
 * @return mixed
 * 查询消息的用户头像
 */
function getMsgCover($user_id,$user_type){
    $table = $user_type == 1 ? "patient" : "doctor";
    $userCover = M($table)->where(array('status'=>1,'id'=>$user_id))->getField('cover');
    return $userCover;
}

/***
 * @param $id
 * @return mixed
 */
function getdemartmentname($id){
     $where['id']=$id;
     $where['status']=1;
     $departmentModel=new \Admin\Model\DepartmentinfoModel();
     $name=$departmentModel->where($where)->getField('name');
     return $name;
}

/**
 * @param $birthday
 * @return bool|false|string
 * 根据出生日期求年龄
 */
function birthday2($birthday){
    if($birthday){
        list($year,$month,$day) = explode("-",$birthday);
        $year_diff = date("Y") - $year;
        $month_diff = date("m") - $month;
        $day_diff  = date("d") - $day;
        if ($day_diff < 0 || $month_diff < 0)
          $year_diff--;
        if($year_diff<0){
            $year_diff=0;
        }
        return $year_diff;
    }else{
        return false;
    }
}

/***
 * @param $type
 * @param $id
 * @return mixed
 */
function getusername($type,$id){

    if($type==1){
       $DoctorInfoModel=new \Admin\Model\DoctorInfoModel();
        $name=$DoctorInfoModel->where('id='.$id)->getField('realname');
    }elseif($type==2){
        $PatientInfoModel=new \Admin\Model\PatientInfoModel();
        $name=$PatientInfoModel->where('id='.$id)->getField('realname');
    }

    return $name;
}

/***根据用户类型获取相对应的用户
 * @param $typid
 * @return mixed
 */
function getpatientList($typid,$id){
    $PatientInfoModel=new  \Admin\Model\PatientInfoModel();
    $DoctorInfoModel=new \Admin\Model\DoctorInfoModel();
    $where['status']=1;
    if(!$id){
       if ($typid) {
        $data=$PatientInfoModel->where($where)->select();
     }else{
           $data=$DoctorInfoModel->where($where)->select();
    }
     }else{
     $where['id']=$id;
     if($typid){
            $data=$PatientInfoModel->where($where)->find();
       }else{
        $data=$DoctorInfoModel->where($where)->find();
     }
   }
     return $data;
}

/***根据用户类型获取相对应的用户
 * @param $typid
 * @return mixed
 */
function getpatientLists($typid,$id){
    $PatientInfoModel=new  \Admin\Model\PatientInfoModel();
    $DoctorInfoModel=new \Admin\Model\DoctorInfoModel();
    $where['status']=1;
    if(!$id){
        if (!$typid) {
            $data=$PatientInfoModel->where($where)->select();
        }else{
            $data=$DoctorInfoModel->where($where)->select();
        }
    }else{
        $where['id']=$id;
        if(!$typid){
            $data=$PatientInfoModel->where($where)->find();
        }else{
            $data=$DoctorInfoModel->where($where)->find();
        }
    }
//    dump($data);
    return $data;

}
/**
 * @param $id
 * @return mixed
 */
function gethospitalname($id){
    $HospitalinfoModel=new \Admin\Model\HospitalinfoModel();
    $data=$HospitalinfoModel->where('status=1')->find($id);
    return $data['name'];
}

/**
 * @param $id
 * @return mixed
 */
function getInspectionDetailInfo($id){

    $infomodel=  new \Admin\Model\InspectiondetailinfoModel();
    $list=$infomodel->relation(true)->where(array('medicalid'=>$id,'status'=>1))->order('id desc')->select();
//    dump($list);
     return $list;
}

/**
 * @param $indexid
 * @param $itemid
 * @return mixed
 * 查看化验单的图片集
 */
function getInspectionDetailImg($indexid,$itemid){
    $InspectionPicDetailModel=new \Admin\Model\InspectionPicDetailModel();
    if($indexid){
        $where['indexid']=$indexid;
    }
    if($itemid){
        $where['itemid']=$itemid;
    }
//     $where['status']=array('egt','0');
      return $InspectionPicDetailModel->where($where)->select();
}

/**
 *
 */
function  getInspectioninfo($id){
    $InspectioninfoModel=new \Admin\Model\InspectioninfoModel();
    if($id){
        $where['id']=$id;
        $where['status']=1;
        $name=$InspectioninfoModel->where($where)->find();
        return $name['name'];
    }else{
        return false;
    }
}

/**
 * @param $hid
 * @return mixed
 */
function getInspectioninfoList($hid){
    $InspectioninfoModel=new \Admin\Model\InspectioninfoModel();
    if($hid){
        $where['hospitalid']=$hid;
        $where['status']=1;
    }
    $list=$InspectioninfoModel->where($hid)->select();
    return $list;
}
function getiteminfo($id){
     $InspectioniteminfoModel=new \Admin\Model\InspectioniteminfoModel();
     $info= $InspectioniteminfoModel->find($id);
     return $info;
}
function getlists($table){
   $list= M($table)->where('status=1')->select();
   return $list;

}
function getdefaultlist($id){
    $DefaultInspectioninfoModel=new \Admin\Model\DefaultInspectioninfoModel();
    $where['status']=1;
    $where['hospital_id']=$id;
    $list=$DefaultInspectioninfoModel->relation(true)->where($where)->select();
   // dump($list);
    return $list;
}

/**
 * @param $id
 * @return mixed
 */
function getfildname($tablename,$id){
    if(!$id){
        return "";
    }
   $name= D($tablename)->where('id='.$id)->getField('name');
   return $name;
}

/**
 * 根据状态获取患者列表
 */
function patientLists($status){
    $DoctorPatientRelativeModel= new \Admin\Model\DoctorPatientRelativeModel();
    $where['relationtype']=$status;
    $where['doctorid']=session('userId');
    $list= $DoctorPatientRelativeModel->pageToView(1,true,$where);
    return $list;
}
/**
 *根据
 */
function getinspectionid($name){
    $condition = new \stdClass();
    $condition->name=$name;
    $condition->status=1;
    return D('inspectioninfo')->where($condition)->getField('id');

}

/**
 * @param $id
 * @param $indexid
 * @return mixed
 */
function getinspectioncontentlist($id,$indexid){
     $condition = new \stdClass();
     $condition->id=$id;
     $InspectioncontentModel= new  \Admin\Model\InspectioncontentModel();
     $name=D('inspectioninfo')->where($condition)->getField('name');
     $where = new stdClass();
     $where->inspectionName=$name;
     $where->indexId=$indexid;
     $list=$InspectioncontentModel->where($where)->select();
    return  $list;
}

/**
 * @param $indexid
 * @return mixed
 *获取相对应的化验单图片
 */
function getinspectioncontentlistpic($indexid){
    $InspectionPicDetailModel=new  \Admin\Model\InspectionPicDetailModel();
    $where['indexid']=$indexid;
   return  $InspectionPicDetailModel->where($where)->select();

}